/**
 * 
 */
package com.jdy.haoduoaiteacher.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.jdy.haoduoaiteacher.activity.ChoiceActivity;
import com.jdy.haoduoaiteacher.model.AlbumCollectionItem;
import com.jdy.haoduoaiteacher.model.CartoonCollectionItem;
import com.jdy.haoduoaiteacher.model.CategoryItem;
import com.jdy.haoduoaiteacher.util.ImageLoader;
import com.jdy.haoduoaiteacher.util.Tools;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.R;

import java.util.ArrayList;

/**
 * @author zp 2016-5-24 下午2:09:44
 */

/**
 * @author zp 2016-4-11 下午5:13:15
 */
public class AnimationClassAdapter extends BaseAdapter {

  private final Context mContext;

  private final LayoutInflater mInflater;

  private final ArrayList<CartoonCollectionItem> mCatelogList;

  public AnimationClassAdapter(Context context,
      ArrayList<CartoonCollectionItem> list) {
    this.mContext = context;
    this.mCatelogList = list;
    this.mInflater = LayoutInflater.from(mContext);
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.widget.Adapter#getCount()
   */
  @Override
  public int getCount() {
    // TODO Auto-generated method stub
    if (this.mCatelogList != null)
      return this.mCatelogList.size();
    else
      return 0;
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.widget.Adapter#getItem(int)
   */
  @Override
  public Object getItem(int position) {
    // TODO Auto-generated method stub
    if (this.mCatelogList != null)
      return this.mCatelogList.get(position);
    else
      return null;
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.widget.Adapter#getItemId(int)
   */
  @Override
  public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
  }

  private CategoryItem convert2CI(CartoonCollectionItem cci) {
    CategoryItem ci = new CategoryItem();
    ci.id = cci.id;
    ci.name = cci.name;
    ci.code = cci.code;
    if (cci.enabled)
      ci.enabled = 1;
    else
      ci.enabled = 0;
    ci.sort = cci.sort;
    ci.created_at = cci.created_at;
    ci.updated_at = cci.updated_at;
    ci.deleted_at = cci.deleted_at;
    ci.icon = cci.icon;
    ci.catalog = cci.catalog;
    ci.home_page = cci.home_page;
    return ci;
  }

  private ImageLoader mImageLoader;

  // private SparseArray<View> cvArray = new SparseArray<>();

  /*
   * (non-Javadoc)
   * 
   * @see android.widget.Adapter#getView(int, android.view.View,
   * android.view.ViewGroup)
   */
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    final CartoonCollectionItem cci = mCatelogList.get(position);
    ViewHolder holder;
    // convertView = cvArray.get(position);
    if (convertView == null) {
      holder = new ViewHolder();
      convertView = mInflater.inflate(R.layout.cartoonlist_gridview_item_teacher, null);
      holder.mIconImg = (ImageView) convertView.findViewById(R.id.icon_img);
      holder.mTitleTv = (TextView) convertView
          .findViewById(R.id.catelog_title_tv);
      holder.mMoreImg = (ImageView) convertView.findViewById(R.id.more_img);
      holder.mGridView = (GridView) convertView
          .findViewById(R.id.story_griditem);

      // if (Tools.isNotEmpty(cci.getAlbum_collection())) {
      // ArrayList<AlbumCollectionItem> aciList = cci.getAlbum_collection();
      // int size = Math.min(6, aciList.size());
      // ArrayList<AlbumCollectionItem> aciList2 = new
      // ArrayList<AlbumCollectionItem>(
      // aciList.subList(0, size));
      // AnimationGridViewAdapter agva = new AnimationGridViewAdapter(mContext,
      // aciList2);
      // holder.mGridView.setAdapter(agva);
      // agva.notifyDataSetChanged();
      // }
      convertView.setTag(holder);
      // cvArray.put(position, convertView);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }
    holder.mTitleTv.setText(cci.name);

    if (Tools.isNotEmpty(cci.album_collection)) {
      ArrayList<AlbumCollectionItem> aciList = cci.album_collection;
      int size = Math.min(6, aciList.size());
      ArrayList<AlbumCollectionItem> aciList2 = new ArrayList<AlbumCollectionItem>(
          aciList.subList(0, size));
      AnimationGridViewAdapter agva = new AnimationGridViewAdapter(mContext,
          aciList2);
      holder.mGridView.setAdapter(agva);
      // Tools.setListViewHeightBasedOnChildren(holder.mGridView)
      agva.notifyDataSetChanged();
    }

    final ImageView imgTmp = holder.mIconImg;
    if (Tools.isNotEmpty(cci.icon))
      Picasso.with(mContext).load(cci.icon)
          .into(holder.mIconImg, new Callback() {

            @Override
            public void onSuccess() {
              // TODO Auto-generated method stub

            }

            @Override
            public void onError() {
              if (mImageLoader == null) {
                mImageLoader = new ImageLoader(mContext);
              }
              mImageLoader.DisplayImage(cci.icon, imgTmp, false);

            }
          });
    holder.mMoreImg.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        CategoryItem categoryItem = convert2CI(cci);
        if (categoryItem.name.equals("经典绘本")) {
          // Intent intent = new Intent();
          // Bundle bundle = new Bundle();
          // intent.setClass(mContext, IllustratedStoryActivity.class);
          // bundle.putSerializable("categoryitem", categoryItem);
          // intent.putExtras(bundle);
          // mContext.startActivity(intent);
        } else {
          Intent intent = new Intent();
          Bundle bundle = new Bundle();
          intent.setClass(mContext, ChoiceActivity.class);
          bundle.putSerializable("categoryitem", categoryItem);
          intent.putExtras(bundle);
          mContext.startActivity(intent);
        }
      }
    });

    return convertView;
  }

  private class ViewHolder {

    public ImageView mIconImg;

    public TextView mTitleTv;

    public ImageView mMoreImg;

    public GridView mGridView;

  }

}
